package cn.woochen.mvvmframe.util

import android.app.Activity
import android.content.Context
import android.content.DialogInterface
import androidx.annotation.LayoutRes
import cn.woochen.comlib.dialog.AlertDialog
import cn.woochen.mvvmframe.R

/**
 * 通用确弹窗
 * @author woochen
 * @time 2018/8/30 9:57
 */

object SimpleDialogUtil {

    /**
     * 显示弹窗
     */
    fun create(
        context: Context, @LayoutRes layoutRes: Int = R.layout.dialog_common, dismissListener: DialogInterface.OnDismissListener? = null, canceledOnTouchOutside: Boolean = true, fromBottom: Boolean = false, styleAnimation: Int = 0, windowWidth: Int = 0, windowHeight: Int = 0
    ): AlertDialog? {
        if (context is Activity && !context.isFinishing) {
            val builder = AlertDialog.Builder(context).setContentView(layoutRes)
            if (fromBottom) builder.fromBottom(fromBottom)
            if (windowWidth != 0 && windowHeight != 0) builder.setWidthAndHeight(
                windowWidth, windowHeight
            )
            if (styleAnimation > 0) builder.addAnimation(styleAnimation)
            val dialog = builder.create()
            if (dismissListener != null) dialog.setOnDismissListener(dismissListener)
            dialog.setCancelable(canceledOnTouchOutside)
            dialog.setCanceledOnTouchOutside(canceledOnTouchOutside)
            return dialog
        }
        return null
    }


}
